package pe.com.bbva.mantenimiento.dao.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pe.com.bbva.mantenimiento.dao.TipoTarjetaDAO;
import pe.com.bbva.mantenimiento.domain.TipoTarjeta;
import pe.com.bbva.util.Constantes;
import pe.com.stefanini.core.dao.GenericDAOImpl;
import pe.com.stefanini.core.exceptions.BOException;
import pe.com.stefanini.core.exceptions.DAOException;

@Service("tipoTarjetaDAO")
public class TipoTarjetaDAOImpl extends GenericDAOImpl<TipoTarjeta> implements TipoTarjetaDAO{
	
	@Autowired
	public TipoTarjetaDAOImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	@SuppressWarnings("unchecked")
	public List<TipoTarjeta> findTipos(TipoTarjeta tipoTarjeta)throws BOException, DAOException {
		String where = "";
		if(tipoTarjeta.getBin()!= null && tipoTarjeta.getBin().getId() != null){
			where = where + (where==""?"":" AND ") + "bin = '" +	tipoTarjeta.getBin().getId() + "'";
		}
		if(tipoTarjeta.getCodTarjeta()!= null && tipoTarjeta.getCodTarjeta().getId() != null ){
			where = where + (where==""?"":" AND ") + "codTarjeta = '" +	tipoTarjeta.getCodTarjeta().getId() + "'";
		}
		if(tipoTarjeta.getTamTarjeta()!= null && tipoTarjeta.getTamTarjeta().getId() != null){
			where = where + (where==""?"":" AND ") + "tamTarjeta = '" +	tipoTarjeta.getTamTarjeta().getId() + "'";
		}
		if(tipoTarjeta.getCodTipoTarjeta()!= null && !tipoTarjeta.getCodTipoTarjeta().equals("")){
			where = where + (where==""?"":" AND ") + "upper(codTipoTarjeta) like upper('%" + tipoTarjeta.getCodTipoTarjeta() + "%')";
		}
		if(tipoTarjeta.getDescripcion()!= null && !tipoTarjeta.getDescripcion().equals("")){
			where = where + (where==""?"":" AND ") + "upper(descripcion) like upper('%" + tipoTarjeta.getDescripcion() + "%')";
		}
		if(tipoTarjeta.getEstado()!= null && !tipoTarjeta.getEstado().equals("")){
			where = where + (where==""?"":" AND ") + "estado = '" + tipoTarjeta.getEstado() + "'";
		}
		if(tipoTarjeta.getId()!= null && !tipoTarjeta.getId().equals("")){
			where = where + (where==""?"":" AND ") + "id = '" +	tipoTarjeta.getId() + "'";
		}
		if(!where.equals("")){
			where = Constantes.WHERE_CLAUSE + where;
		}
		String orders = " order by fechaCreacion desc";
		List<TipoTarjeta> listaTipoTarjeta = super.executeQuery(TipoTarjeta.class,where,orders);
		return listaTipoTarjeta;
	}
	
	@SuppressWarnings("deprecation")
	public Long ValidaCargaEnProceso() throws DAOException {
		Session hibernateSession = null;
		Connection con = null;
		CallableStatement cstmt =null;
		ResultSet rs  = null;		
		Long IdCarga=0L;
		try {
			hibernateSession = (Session) getHibernateTemplate().getSessionFactory().openSession();
			con = hibernateSession.connection();
			cstmt =con.prepareCall("{call TARGPS.PKG_TARGPS.PR_VALIDAR_ENPROCESO(?)}");			
			cstmt.registerOutParameter(1,oracle.jdbc.OracleTypes.VARCHAR);		
			cstmt.executeQuery();			
			IdCarga = Long.parseLong((String)cstmt.getObject(1));			
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DAOException(e.getMessage());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new DAOException(e.getMessage());
		}finally{
			try {
				if(rs!=null){
					rs.close();
				}
				if(cstmt!=null){
					cstmt.close();
				}
				hibernateSession.close();
			} catch (SQLException e) {
				throw new DAOException(e.getMessage());
			} catch (HibernateException e) {
				e.printStackTrace();
				throw new DAOException(e.getMessage());
			} 
		}
		return IdCarga;
	}
}